/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;


import domain.Insurancecontract;
import domain.Userdom;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class InsuranceContractDao {
    EntityManager em;
    EntityTransaction tx;

    public InsuranceContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Insurancecontract getInsuranceContract(Integer idInsuranceContract) {
        Insurancecontract i = (Insurancecontract) em.createNativeQuery("select * from insurancecontract as n where n.idInsuranceContract='" + idInsuranceContract + "'", Insurancecontract.class).getSingleResult();
        return i;
    }

    public void saveInsuranceContract(Insurancecontract i) {
        tx.begin();
        em.persist(i);
        tx.commit();
    }

    public void removeInsuranceContract(Insurancecontract i) {
        tx.begin();
        em.remove(i);
        tx.commit();
    }

    public List<Insurancecontract> getInsContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, contract c, insurancecontract i where b.userFK='"+user.getNick()+"' and c.bankFK=b.idBank and i.contractFK=c.idContract", Insurancecontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateInsuranceContract(Insurancecontract i) {
        tx.begin();
        em.createNativeQuery("update insurancecontract set  dateOfExpiration='"+i.getDateOfExpiration()+"', company='"+i.getCompany()+"' where idInsuranceContract='"+i.getIdInsuranceContract()+"'").executeUpdate();
        tx.commit();
    }
}
